package capture;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

public class MainPanel extends JFrame implements ActionListener {

    private JPanel c;
    /** 一个放置很多份图片  */
    private JTabbedPane jtp;
    /** 一个一直会递增的索引,用于标认图片 */
    private int index;
    private JButton start, cancel;
    /** JAVA界面,系统界面 */
    private JRadioButton java, system;

    public MainPanel() {
        super("屏幕截取");

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exe) {
            exe.printStackTrace();
        }

        initWindow();
        initOther();
    }

    private void initOther() {
        jtp = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
    }

    private void initWindow() {
        start = new JButton("开始截取");
        cancel = new JButton("退出");
        start.addActionListener(this);
        cancel.addActionListener(this);
        JPanel button = new JPanel();
        c = new JPanel(new BorderLayout());
        JLabel jl = new JLabel("屏幕截取", JLabel.CENTER);
        jl.setFont(new Font("黑体", Font.BOLD, 40));
        jl.setForeground(Color.RED);
        c.add(jl, BorderLayout.CENTER);
        button.add(start);
        button.add(cancel);
        button.setBorder(BorderFactory.createTitledBorder("公共操作区"));
        // 放置两个单选按钮的面板
        JPanel jp = new JPanel();
        jp.add(java = new JRadioButton("Java界面"));
        jp.add(system = new JRadioButton("系统界面", true));
        java.addActionListener(this);
        system.addActionListener(this);
        jp.setBorder(BorderFactory.createTitledBorder("界面风格"));
        ButtonGroup bg = new ButtonGroup();
        bg.add(java);
        bg.add(system);
        JPanel all = new JPanel();
        all.add(jp);
        all.add(button);
        this.getContentPane().add(c, BorderLayout.CENTER);
        this.getContentPane().add(all, BorderLayout.SOUTH);
        this.setSize(500, 400);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        Object source = ae.getSource();
        if (source == start) {
            doStart();
        } else if (source == cancel) {
            System.exit(0);
        } else if (source == java) {
            // 金属外观
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                SwingUtilities.updateComponentTreeUI(this);
            } catch (Exception exe) {
                exe.printStackTrace();
            }
        } else if (source == system) {
            // 本地外观
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                SwingUtilities.updateComponentTreeUI(this);
            } catch (Exception exe) {
                exe.printStackTrace();
            }
        }
    }

    private void doStart() {
        try {
            this.setVisible(false);
            //睡500毫秒是为了让主窗完全不见
            Thread.sleep(500);
            // （通过本地操作）控制鼠标、键盘等实际输入源（java.awt）
            Robot ro = new Robot();
            // AWT组件的抽象父类（java.awt）
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension di = tk.getScreenSize();
            Rectangle rec = new Rectangle(0, 0, di.width, di.height);
            BufferedImage bi = ro.createScreenCapture(rec);
            JFrame jf = new JFrame();
            // 自定义的Temp类的对象
            CapPanel capPanel = new CapPanel(this, jf, bi, di.width, di.height);
            jf.getContentPane().add(capPanel, BorderLayout.CENTER);
            jf.setUndecorated(true);
            jf.setSize(di);
            jf.setVisible(true);
            jf.setAlwaysOnTop(true);
        } catch (Exception exe) {
            exe.printStackTrace();
        }
    }

    void updates(BufferedImage captureImage) {
        this.setVisible(true);
        if (captureImage != null) {
            //如果索引是0,则表示一张图片都没有被加入过,
            //则要清除当前的东西,重新把tabpane放进来
            //否则的话,直接对tabpane添加面板就可以了
            //就什么都不用做了
            if (index == 0) {
                c.removeAll();
                c.add(jtp, BorderLayout.CENTER);
            }
            PicPanel pic = new PicPanel(this, captureImage);
            jtp.addTab("图片" + (++index), pic);
            jtp.setSelectedComponent(pic);
            // 调整LookAndFeel（javax.swing）
            SwingUtilities.updateComponentTreeUI(c);
        }
    }

    public void removePicPanel(Component component) {
        jtp.remove(component);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(MainPanel::new);
    }
}
